package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Region;
import cn.wolfcode.luowowo.query.QueryObject;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IRegionService;
import cn.wolfcode.luowowo.util.JsonResult;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("region")
public class RegionController {

    @Autowired
    private IRegionService regionService;

    @Autowired
    private IDestinationService destinationService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        Page page = regionService.query(qo);
        model.addAttribute("page", page);
        return "region/list";
    }

    @RequestMapping("/getDestByDeep")
    @ResponseBody
    public Object getDestByDeep(int deep) {
        List<Destination> list = destinationService.list();
        //应该通过deep去查数据的，但是数据少了
        List<Destination> list1 = destinationService.getByDeep(deep);
        return list1;
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region) {
        if (StringUtils.hasLength(region.getId())) {
            regionService.update(region);
        } else {
            region.setId(null);
            regionService.save(region);
        }
        return JsonResult.success();
    }

    //关联的目的地
    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid) {
        //通过rid查询到数据
        List<Destination> list = destinationService.getDestByRegionId(rid);
        return list;
    }

    //切换热点
    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(String id, int hot) {
        regionService.changeHotValue(id, hot);
        return JsonResult.success();
    }

    //删除区域：一般不删除，改变状态为非正常即可，因为很多数据相关联，删除会出问题的
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        regionService.delete(id);
        return JsonResult.success();
    }


}
